import { toJS, action, observable } from 'mobx';
import { WeaForm, WeaTableNew } from 'comsMobx';
import { WeaLocaleProvider } from "ecCom";
import { message } from 'antd';
import * as Apis from '../apis/odocFinishApi';

const { getLabel } = WeaLocaleProvider;

const TableStore = WeaTableNew.TableStore;

export default class OdocFinish {
  @observable searchForm = new WeaForm();
  @observable conditions = [];
  @observable showSearchAd = false;
  @observable tableStore = new TableStore();
  @observable selectedRowKeys = [];
  @observable RowSelectedInfo = [];
  @observable dialogForm = new WeaForm();
  @observable dialogShow = false;
  @observable ismultiple = '0';
  @observable isLookOver = false;
  @observable IsCheckRight = false;
  /**
   * 权限判断，判断是否拥有模板维护权限
   *
   */
  @action.bound
  checkUserRight = (query) => {
    Apis.checkright(query).then((result) => {
      if (result) {
        this.IsCheckRight = result.isHaveRight;
      }
    });
  }
  @action.bound
  getSearchForm (query = {}) {
    Apis.getSearchForm(query).then((data) => {
      this.conditions = data.conditions;
      this.searchForm.setCondition(data.conditions);
    });
  }

  @action.bound
  getTableData (formDatas, query = {}) {
    const formData = formDatas || this.searchForm.getFormParams();
    const params = {
      ...formData,
      ...query,
    };
    Apis.getTableData(params).then((data) => {
      if (data.sessionkey) {
        this.tableStore.getDatas(data.sessionkey);
      }
    });
  }

  @action.bound
  setStatus (props) {
    Object.keys(props).forEach((item) => {
      this[item] = props[item];
    });
  }

  @action.bound
  rowSelected (selectedKeys, selectedRows) {
    this.selectedRowKeys = [];
    this.RowSelectedInfo = [];
    toJS(selectedRows).map((item) => {
      this.RowSelectedInfo.push({ name: item.docsubject, docid: item.docid, requestid: item.randomFieldId });
      this.selectedRowKeys.push(item.docid);
    });
  }

  @action.bound
  getDialogForm (id) {
    let params = {
      docid: this.selectedRowKeys.join(','),
      ismultiple: this.ismultiple,
    };
    if (id) {
      params.requestid = id;
    }
    Apis.getDialogForm(params).then((data) => {
      this.dialogForm = new WeaForm();
      if (this.ismultiple == '1') {
        data.conditions[0].items[4].format = 'YYYY';
      } else {
        data.conditions[0].items[7].format = 'YYYY';
      }
      this.dialogForm.setCondition(data.conditions);
    });
  }

  @action.bound
  showDialog (ismultiple = '0', requestid, isLookOver = false) {
    this.ismultiple = ismultiple;
    this.isLookOver = isLookOver;
    this.getDialogForm(requestid);
    this.dialogShow = true;
  }

  @action.bound
  closeDialog () {
    this.selectedRowKeys = [];
    this.RowSelectedInfo = [];
    this.tableStore.setSelectedRowKeys([]);
    this.dialogShow = false;
  }

  @action.bound
  onOperatesClick (record, index, operate, flag) {
    if (flag == 0) {
      this.selectedRowKeys.push(record.docid);
      this.RowSelectedInfo.push({ name: record.requestnamespan, docid: record.docid, requestid: record.randomFieldId });
      this.showDialog(undefined, undefined, false);
    } else if (flag == 1) {
      this.selectedRowKeys.push(record.docid);
      this.RowSelectedInfo.push({ name: record.requestnamespan, docid: record.docid, requestid: record.randomFieldId });
      this.showDialog('0', record.randomFieldId, true);
    }
  }

  @action.bound
  odocArchive () {
    this.dialogForm.validateForm().then((f) => {
      if (f.isValid) {
        let requestidArr = [];
        const formData = this.dialogForm.getFormParams();
        this.RowSelectedInfo.map((item) => {
          if (this.selectedRowKeys.indexOf(item.docid) > -1) {
            requestidArr.push(item.requestid);
          }
        });
        const params = {
          requestid: requestidArr.join(','),
          ...formData,
        };
        Apis.odocArchive(params).then((data) => {
          if (data.api_status) {
            this.closeDialog();
            this.getTableData();
            message.success(getLabel('归档成功', '归档成功'));
          }
        });
      } else {
        f.showErrors();
      }
    });
  }

  @action.bound
  dialogCheck (docid) {
    const index = toJS(this.selectedRowKeys).indexOf(docid);
    const hasKey = index > -1;
    if (hasKey) {
      this.selectedRowKeys.splice(index, 1);
    } else {
      this.selectedRowKeys.push(docid);
    }
    this.dialogForm.updateFields({
      allFileNum: {
        value: this.selectedRowKeys.length,
        valueSpan: this.selectedRowKeys.length,
      },
    });
  }

  @action.bound
  getSearchData () {
    this.showSearchAd = false;
    this.getTableData();
  }

  // 显示列定制
  @action.bound
  customSet () {
    this.tableStore.setColSetVisible(true);
    this.tableStore.tableColSet(true);
  }
}
